﻿using System.Windows;
using System.Windows.Media;
using DCT.ViewModel;

namespace DCT.View
{
    public class SwitchView : DeviceBaseView
    {
        #region Constructor

        public SwitchView()
            : base()
        {
        }
                     
        #endregion // Constructor

        #region Fields

        SwitchViewModel _swViewModel;

        #endregion // Fields

        #region Methods

        public override void UpdateVisual()
        {            
            if (_swViewModel.SwViewModelType == SwitchViewModelType.Merge)
            {
                base.UpdateVisual();

                if (_deviceVisual.Children.Count == 2)
                {
                    // 注意删除的顺序
                    _deviceVisual.Children.RemoveAt(1);
                    _deviceVisual.Children.RemoveAt(0);
                }


                Point swMergePoint = new Point((this.Width + 1) / 2, (this.Height + 1) / 2);
                Point swNormalPoint = new Point(_swViewModel.NormalSwViewModel.PositionPoint.X - _swViewModel.PositionPoint.X + swMergePoint.X,
                                                _swViewModel.NormalSwViewModel.PositionPoint.Y - _swViewModel.PositionPoint.Y + swMergePoint.Y);
                Point swReversePoint = new Point(_swViewModel.ReverseSwViewModel.PositionPoint.X - _swViewModel.PositionPoint.X + swMergePoint.X,
                                                _swViewModel.ReverseSwViewModel.PositionPoint.Y - _swViewModel.PositionPoint.Y + swMergePoint.Y);

                //Normal Line
                DrawingVisual drawingVisual = new DrawingVisual();
                DrawingContext drawingContext = drawingVisual.RenderOpen();
                drawingContext.DrawDrawing(new GeometryDrawing(Brushes.Black,
                                                               new Pen(Brushes.Yellow, 5),
                                                               new LineGeometry(swMergePoint, swReversePoint)));
                drawingContext.Close();
                _deviceVisual.Children.Add(drawingVisual);

                //Reverse Line
                drawingVisual = new DrawingVisual();
                drawingContext = drawingVisual.RenderOpen();
                drawingContext.DrawDrawing(new GeometryDrawing(Brushes.Black,
                                                               new Pen(Brushes.LimeGreen, 5),
                                                               new LineGeometry(swMergePoint, swNormalPoint)));
                drawingContext.Close();
                _deviceVisual.Children.Add(drawingVisual);
            }
            else
            {
                if (_swViewModel.MergeSwViewModel != null)
                {
                    _swViewModel.MergeSwViewModel.UpdateVisual();
                }
            }
        }

        protected override void DeviceView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            base.DeviceView_DataContextChanged(sender, e);

            if (e.NewValue is SwitchViewModel)
            {
                _swViewModel = e.NewValue as SwitchViewModel;
                _swViewModel.UpdateVisual += UpdateVisual;
            }
        }

        #endregion // Methods
    }
}
